#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import traceback
from fastapi import APIRouter

from app.core.logging import get_logger

# 创建日志记录器
logger = get_logger("api_router")

# 创建主路由
api_router = APIRouter()

# 添加一个测试路由
@api_router.get("/test", tags=["test"])
async def test_api():
    """测试API是否正常工作"""
    logger.info("访问测试端点")
    return {"status": "ok", "message": "API正常工作"}

# 添加一个直接的项目测试路由
@api_router.get("/projects-test", tags=["test"])
async def projects_test():
    """测试项目路由是否正常工作"""
    return {"status": "ok", "message": "项目路由正常工作"}

logger.info("API路由初始化完成")

# 尝试导入和注册各个模块的路由
try:
    from app.api.endpoints import projects
    api_router.include_router(projects.router, prefix="/projects", tags=["projects"])
    logger.info("已加载项目管理路由")
except Exception as e:
    logger.error(f"加载项目管理路由失败: {str(e)}")
    logger.error(traceback.format_exc())

try:
    from app.api.endpoints import text2sql
    api_router.include_router(text2sql.router, tags=["text2sql"])
    logger.info("已加载文本到SQL路由")
except Exception as e:
    logger.error(f"加载文本到SQL路由失败: {str(e)}")
    logger.error(traceback.format_exc())

try:
    from app.api.endpoints import correction
    api_router.include_router(correction.router, tags=["correction"])
    logger.info("已加载SQL纠错路由")
except Exception as e:
    logger.error(f"加载SQL纠错路由失败: {str(e)}")
    logger.error(traceback.format_exc())

try:
    from app.api.endpoints import training_data
    api_router.include_router(training_data.router, tags=["training_data"])
    logger.info("已加载训练数据路由")
except Exception as e:
    logger.error(f"加载训练数据路由失败: {str(e)}")
    logger.error(traceback.format_exc())

try:
    from app.api.endpoints import history
    api_router.include_router(history.router, tags=["history"])
    logger.info("已加载查询历史路由")
except Exception as e:
    logger.error(f"加载查询历史路由失败: {str(e)}")
    logger.error(traceback.format_exc())

# simple_projects 已删除，使用 projects 端点

try:
    from app.api.endpoints import health
    api_router.include_router(health.router, tags=["health"])
    logger.info("已加载健康检查路由")
except Exception as e:
    logger.error(f"加载健康检查路由失败: {str(e)}")
    logger.error(traceback.format_exc())